package com.banzhi.shop.model.Impl;

import com.google.gson.Gson;
import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.GoodsDetails.GoodsDetails;
import com.banzhi.shop.model.CallBack.CommodityDetailsCallBack;
import com.banzhi.shop.model.ICommodityDetailsModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by HT on 2017/5/3.
 * 商品详情页
 */

public class CommodityDetailsImpl implements ICommodityDetailsModel {
    /**
     * @param goods_id
     * @param callBack 获取商品详情页信息
     */
    @Override
    public void getData(final String goods_id, String action, final CommodityDetailsCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.goodsDetailUrl);
        params.addBodyParameter("goods_id", goods_id);
        params.addBodyParameter("action", action);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("CommodityDetailsImpl", params.toString()+"action="+action);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        GoodsDetails data = gson.fromJson(result, GoodsDetails.class);
                        callBack.ongetDataSuccess(data.getGoods());
                    } else {
                        callBack.ongetDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("CommodityDetailsImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(4444)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }


}
